package com.zmn.oms.services.impl.worktrack;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.oms.model.entity.omstrack.OrderWorkTrack;
import com.zmn.oms.persistence.interfaces.worktrack.WorkTrackDao;
import com.zmn.oms.services.interfaces.worktrack.WorkTrackService;
import io.shardingsphere.api.HintManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName OmsTrackServiceImpl
 * @description:
 * @author: wangyanci
 * @create: 2021-05-12 18:19
 * @Version 1.0
 **/
@Service
@Slf4j
public class WorkTrackServiceImpl implements WorkTrackService {

    @Resource
    private WorkTrackDao omsTrackDao;


    public void insert(OrderWorkTrack orderWorkTrack) {
        omsTrackDao.insert(orderWorkTrack);
    }

    public void delete(Long orderId, Long workId, Integer trackType) {
        log.info("OmsTrackServiceImpl.delete orderId={},workId={},trackType={}", orderId, workId, trackType);
        omsTrackDao.delete(orderId, workId, trackType);
    }

    public OrderWorkTrack getWorkTrackByTrackTypeSrcMaster(Long orderId, Long workId, Integer trackType) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return omsTrackDao.getWorkTrackByTrackType(orderId, workId, trackType);
        }
    }

    /**
     * 根据订单id、工单id、跟单类型查询跟单
     * @param orderId
     * @param workId
     * @param trackType
     * @return
     */
    @Override
    public OrderWorkTrack getWorkTrackByTrackType(Long orderId, Long workId, Integer trackType) {
        return omsTrackDao.getWorkTrackByTrackType(orderId, workId, trackType);
    }

    @Override
    public List<OrderWorkTrack> queryListByOrderIdSrcMaster(Long orderId, Long workId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return omsTrackDao.queryListByOrderId(orderId, workId);
        }
    }

    @Override
    public List<OrderWorkTrack> queryListByOrderIdsSrcMaster(Collection<Long> orderIds) {
        if (CollectionUtil.isNullOrEmpty(orderIds)) {
            return null;
        }
        // log.info("OmsTrackServiceImpl.queryListByOrderIds orderIds={}", orderIds);
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return omsTrackDao.queryListByOrderIds(orderIds);
        }
    }

    @Override
    public List<OrderWorkTrack> getWorkTrackByTrackTypeList(Long orderId, Long workId, List<Integer> trackTypeList) {
        return omsTrackDao.getWorkTrackByTrackTypeList(orderId, workId, trackTypeList);
    }

    @Override
    public List<OrderWorkTrack> getWorkTrackByTrackTypeListSrcMaster(Long orderId, Long workId, List<Integer> trackTypeList) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return omsTrackDao.getWorkTrackByTrackTypeList(orderId, workId, trackTypeList);
        }
    }
}